<style type="text/css">
<!--
a img{
  border: 0;
}
.panel{
  background: #F0F0F0;
  height: 24px;
  width: 100%;
  padding: 1px;
}
.panel a.buttn{
  display: block;
  width:20px;
  height: 20px;
  padding: 1px;
  float: left;
  border: solid #F0F0F0 1px;
  background: #F0F0F0;
}
.panel a.buttn:hover{
  border: solid #cccccc 1px;
}
.areadiv{
}
.htmlarea{
  border: 1px solid #CCCCCC;
  background: #FFFFFF;
  color: #000000;
  width: 100%;
  height: 300px;
}
.addimgdiv{
  width: 100%;
  border: dashed #cccccc 1px;
  padding: 10px;
  padding-top: 0px;
  display: none;
}
.addimgdiv form
{
  padding: 0px;
  margin: 0px;
}
.addimgdiv label{
  margin-top: 5px;
  display: block;
}
.addimgdiv .buttns{
  padding-top: 10px;
}
-->
</style>

<script type="text/javascript">
<!--
var mouseY;
var textarea_height;
var undo='';
var quote=false;

//undo
function text_undo(){
  if (undo) document.getElementById("htmlarea").value=undo;
  document.getElementById("undoimg").src="/images/editor/undo_grey.gif";
}
function text_save_undo(){
  undo=document.getElementById("htmlarea").value;
  document.getElementById("undoimg").src="/images/editor/undo.gif";
}
function text_clear_undo(){
  undo='';
  document.getElementById("undoimg").src="/images/editor/undo_grey.gif";
}
//

//cursor
function getCursor(input)
{
  var result = { start: 0, end: 0 };
  if (input.setSelectionRange)
  {
    result.start= input.selectionStart;
    result.end = input.selectionEnd;
  }
  else if (!document.selection) { return 0; }
  else if (document.selection && document.selection.createRange)
  {

    var range = document.selection.createRange();
    var stored_range = range.duplicate();
    stored_range.moveToElementText(input);
    stored_range.setEndPoint('EndToEnd', range);
    result.start = stored_range.text.length - range.text.length;
    result.end = result.start + range.text.length; //}


  }
  return result;
}
function setCursor(txtarea, start, end){

  if(txtarea.createTextRange) {
        var range = txtarea.createTextRange();
        range.move("character", start);
        range.select();
    } else if(txtarea.selectionStart) {
        txtarea.setSelectionRange(start, end);
    }

}

function insert_tag(starttag,endtag){
text_save_undo();

var txtarea = document.getElementById("htmlarea");
txtarea.focus();
var scrtop = txtarea.scrollTop;
var cursorPos=getCursor(txtarea); 

if (cursorPos.start==cursorPos.end)
{
var nuCursorPos=cursorPos.start+starttag.length;
txtarea.value = txtarea.value.substring(0,cursorPos.start)+starttag+endtag+txtarea.value.substr(cursorPos.start );
setCursor(txtarea,nuCursorPos,nuCursorPos);
}
else{
var txt_pre=txtarea.value.substring (0,cursorPos.start);
var txt_sel=txtarea.value.substring(cursorPos.start,cursorPos.end);
var txt_aft=txtarea.value.substring(cursorPos.end);
txtarea.value = txt_pre+starttag+txt_sel+endtag+txt_aft;
var nuCursorPos=String(txt_pre+starttag+txt_sel+endtag).length;
setCursor(txtarea,nuCursorPos,nuCursorPos);
}
if (scrtop) txtarea.scrollTop=scrtop;
}

function insert_text_quote(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);
  if (cur.start!=cur.end){
    insert_tag("&laquo;", "&raquo;");
  }
  else
  {
    document.getElementById("quoteimg").src=quote?"/images/editor/laquo.gif":"/images/editor/raquo.gif";
    insert_tag("", quote?"&raquo;":"&laquo;");
    quote=!quote;
  }
}
function insert_text_mdash(){
  insert_tag("", "&mdash;");
}
function insert_text_blockquote(){
  insert_tag("<blockquote>", "</blockquote>");
}
function insert_text_b(){
  insert_tag("<strong>", "</strong>");
}
function insert_text_youtube(){
  insert_tag("<youtube>", "</youtube>");
}
function insert_text_rutube(){
  insert_tag("<rutube>", "</rutube>");
}
function insert_text_vimeo(){
  insert_tag("<vimeo>", "</vimeo>");
}
function insert_text_img(){

  document.getElementById("picuploadform").style.display="block";
  document.getElementById("screen").style.display="block";
}
function insert_text_img_close(){
  document.getElementById("picuploadform").style.display="none";
  document.getElementById("screen").style.display="none";
}
function insert_text_i(){
  insert_tag("<em>", "</em>");
}
function insert_text_user(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);

  var username=window.prompt("Кто?", '');
  if (username){
    if (cur.start!=cur.end){
       insert_tag('<hh user="'+username+'">', '</hh>');
    }
    else
    {
      insert_tag('', '<hh user="'+username+'" />');
    }
  }
}
function insert_text_u(){
  insert_tag("<u>", "</u>");
}
function insert_text_s(){
  insert_tag("<s>", "</s>");
}
function insert_text_link(){
  var txtarea = document.getElementById("htmlarea");
  var cur = getCursor(txtarea);

  if (cur.start!=cur.end){
    var hreff=window.prompt("Введите URL ссылки", '');
    var regexx = new RegExp('([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)');


    if (hreff && hreff.match(regexx)) {
        insert_tag('<a href="'+hreff+'">', "</a>");
    }
    else alert("Это не URL");
  }
  else{
    alert("Сначала выделите текст, который вы желаете сделать ссылкой");
  }
}
function resize_mousemove(evt){
  if (!evt) evt=this.event;
  if (evt.pageY)
    var newheight=evt.pageY-mouseY;
  else
    var newheight=evt.clientY-mouseY;
  if (newheight>=50){
    document.getElementById("htmlarea").style.height=newheight+"px";
    textarea_height=newheight;
  }
}
function resize_start(evt){
  if (!evt) evt=this.event;
  if (!textarea_height) textarea_height=100;
  if (evt.pageY)
    mouseY=evt.pageY-textarea_height;
  else
    mouseY=evt.clientY-textarea_height;
  if (document.addEventListener){ //GECKO
    document.addEventListener('mousemove',resize_mousemove,false);
  }
  else{//IE
    document.attachEvent('onmousemove',resize_mousemove);
  }
}

function resize_stop(){
  if (document.addEventListener){ //GECKO
    document.removeEventListener('mousemove',resize_mousemove,false);
  }
  else {//IE
    document.detachEvent('onmousemove',resize_mousemove);
  }
}

if (document.addEventListener){ //GECKO
  document.addEventListener('mouseup',resize_stop,false);
}
else {//IE
  document.attachEvent('onmouseup',resize_stop);
}
//
-->
</script>

<div id="picuploadform">
<form style="width: 100%;" target="upframe" action="/api/upload_imges/" method="POST" enctype="multipart/form-data">
<table width="100%" cellspacing="0" cellpadding="5">
	<tr>
		<td width="23%">Изоображение:</td>
		<td width="77%"><input type="file" name="image" style="width: 100%;"></td>
	</tr>
	<tr>
		<td>Ширина:</td>
		<td><input type="text" name="width" value="" style="width: 100px;"><br /><span class="help">По-умолчанию и максимальное значение 400.</span></td>
	</tr>
	<tr>
		<td>Выравнивание:</td>
		<td><select name="align">
				<option value="">нет
				<option value="left">слева
				<option value="center">по центру
				<option value="right">справа
		</select></td>
	</tr>
	<tr>
		<td>Описание:</td>
		<td><input type="text" name="alt" value="" style="width:100%;"></td>
	</tr>
	<tr>
		<td colspan="2">
			<input id="pb1" type="submit" value="Загрузить">
			<input id="pb2" type="button" onClick="return insert_text_img_close();" value="Закрыть">
			<span style="display: none;"><iframe src="about:blank" width="1" height="1" name="upframe"></iframe></span>
		</td>
	</tr>
</table>
</form>
</div>